package com.mlk.reback.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mlk.busPram.constant.BusParamConstant;
import com.mlk.busPram.mapper.EngBusParamMapper;
import com.mlk.busPram.po.EngBusParam;
import com.mlk.constant.EngConstant;
import com.mlk.core.component.page.Page;
import com.mlk.core.component.page.Pager;
import com.mlk.core.exception.GlobalException;
import com.mlk.core.util.ServerResponse;
import com.mlk.core.util.validate.ValidateUtils;
import com.mlk.reback.mapper.EngRebackTicketItemMapper;
import com.mlk.reback.mapper.EngRebackTicketMapper;
import com.mlk.reback.po.EngRebackTicket;
import com.mlk.reback.po.EngRebackTicketItem;
import com.mlk.reback.service.EngRebackTicketService;
import com.mlk.reback.vo.EngRebackTicketVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class EngRebackTicketServiceImpl implements EngRebackTicketService {

    @Resource
    private EngRebackTicketMapper engRebackTicketMapper;

    @Resource
    private EngRebackTicketItemMapper engRebackTicketItemMapper;

    @Resource
    private EngBusParamMapper engBusParamMapper;

    /**
     * 新增
     * @param ticket
     * @return
     */
    @Override
    public ServerResponse addTicket(EngRebackTicketVO ticket) {
        EngRebackTicket bean = new EngRebackTicket();
        BeanUtils.copyProperties(ticket,bean);
        ValidateUtils.validate(bean);

        String ticketId = IdUtil.createSnowflake(5L,5L).nextIdStr();

        // 插入主体内容
        bean.setCreateTime(new Date());
        bean.setTicketId(ticketId);
        int  count = this.engRebackTicketMapper.insert(bean);
        if(count == 0){
            throw new GlobalException("换购预约失败");
        }

        //
        if(CollectionUtils.isEmpty(ticket.getItems())){
            return ServerResponse.ok("换购预约成功");
        }

        // 下面插入明细信息
        ticket.getItems().stream().forEach(element->{
            element.setTicketId(ticketId);
            this.engRebackTicketItemMapper.insert(element);
        });
        return ServerResponse.ok("换购预约成功");
    }

    /**
     * 查询我的换购单
     * @param openId
     * @return
     */
    @Page
    @Override
    public Pager queryMyReback(String openId) {
        EngRebackTicketVO ticket = new EngRebackTicketVO();
        ticket.setOpenId(openId);
        List<EngRebackTicketVO> lists = this.engRebackTicketMapper.selectForPage(ticket);
        lists.stream().forEach(element->{
            element.setStatusName(EngConstant.REBACK_STATUS_REF.adapt(element.getStatus()));
        });
        return  Pager.adapt(lists);
    }

    /**
     * 查询换购单的详情
     * @param ticketId
     * @return
     */
    @Override
    public ServerResponse queryDetail(String ticketId) {
        return ServerResponse.ok(this.engRebackTicketItemMapper.selectList(new EntityWrapper<>(EngRebackTicketItem.builder().ticketId(ticketId).build())));
    }

    /**
     * 分页数据查询
     * @param vo
     * @return
     */
    @Page
    @Override
    public Pager listPage(EngRebackTicketVO vo) {
        return Pager.adapt(this.engRebackTicketMapper.selectForPage(vo));
    }

    /**
     * 查看详情信息
     * @param ticketId
     * @return
     */
    @Page
    @Override
    public Pager ticketDetail(String ticketId) {
        return Pager.adapt(this.engRebackTicketItemMapper.
                selectList(new EntityWrapper<>(EngRebackTicketItem.builder().ticketId(ticketId).build())));
    }

    /**
     * 接收换购
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ServerResponse accept(String id) {
        EngRebackTicket ticket = new EngRebackTicket();
        ticket.setHandlerTime(new Date());
        ticket.setId(Integer.parseInt(id));
        ticket.setStatus(EngConstant.REBACK_STATUS.ACCEPT_STATUS);
        int count = this.engRebackTicketMapper.updateById(ticket);
        if(count ==0){
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }

    /**
     * 完成换购
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ServerResponse finish(String id) {
        // 首先校验是否存在
        EngRebackTicket ticket = this.engRebackTicketMapper.selectById(id);
        // 获取子项内容
        String ticketId = ticket.getTicketId();
        // 判断子项的内容是否都已经在业务参数表之中
        List<EngRebackTicketItem> items = this.engRebackTicketItemMapper.
                selectList(new EntityWrapper<>(EngRebackTicketItem.builder().ticketId(ticketId).build()));

        items.stream().forEach(element->{
            String param = element.getParam();
            int  count = this.engBusParamMapper.selectCount(new EntityWrapper<>(EngBusParam.builder().name(param).type(BusParamConstant.REBACK_PARAM_TYPE).build()));
            if(count == 0){
                throw new GlobalException("参数型号:"+param+"没有在参数列表之中,请添加至参数管理之中或者校正该名称");
            }
        });

        // 最后
        EngRebackTicket bean = new EngRebackTicket();
        bean.setId(Integer.parseInt(id));
        bean.setFinishTime(new Date());
        bean.setStatus(EngConstant.REBACK_STATUS.FINISH_STATUS);
        int count = this.engRebackTicketMapper.updateById(bean);
        if(count == 0){
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }

    /**
     * 添加到业务参数之中
     * @param param
     * @return
     */
    @Override
    @Transactional
    public ServerResponse addBusParam(EngBusParam param) {
        // 判断该参数是否已经在参数列表之中了
        int count = this.engBusParamMapper.selectCount(new EntityWrapper<>(EngBusParam.builder().name(param.getName()).type(BusParamConstant.REBACK_PARAM_TYPE).build()));
        if(count > 0){
            return ServerResponse.ok("操作成功");
        }
        // 添加到参数库
        count = this.engBusParamMapper.insert(param);
        if(count == 0){
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }

    /**
     * 校正业务参数
     * @param item
     * @return
     */
    @Override
    public ServerResponse just(EngRebackTicketItem item) {
        int count = this.engRebackTicketItemMapper.updateById(item);
        if(count == 0){
            throw new GlobalException("操作失败");
        }

        return ServerResponse.ok("操作成功");
    }

    /**
     * 智能输入
     * @param ticket
     * @return
     */
    @Override
    public ServerResponse AIInput(EngRebackTicket ticket) {
        return ServerResponse.ok(this.engRebackTicketMapper.AIInput(ticket));
    }
}
